package com.lyh.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

    //手机号码的正则表达式
    private static final Pattern PHONE = Pattern.compile("^1[3-9]\\d{9}$");
    /**
     * 检测是否为合法的手机号
     */
    public static boolean checkPhone(String string){
        Matcher matcher = PHONE.matcher(string);
        return matcher.matches();
    }

    //验证密码跟用户名, 只能有数字和字母组成
    private static final Pattern USERNAME_PASSWORD = Pattern.compile("^[0-9a-zA-Z]*$");
    public static boolean checkUsernameAndPassword(String string){
        Matcher matcher = USERNAME_PASSWORD.matcher(string);
        return matcher.matches();
    }

    private static final Pattern EMAIL = Pattern.compile("^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$");
    public static boolean checkEmail(String string){
        Matcher matcher = EMAIL.matcher(string);
        return matcher.matches();
    }

    private static final Pattern IDENTIFY = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)$");
    public static boolean checkIdentify(String string){
        Matcher matcher = IDENTIFY.matcher(string);
        return matcher.matches();
    }

    /**
     * 判断字符串是否为null, 或者为空字符串
     */
    public static boolean notNull(String string){
        if(string==null)
            return false;
        return !string.isEmpty();
    }

}
